package org.hgg.workflow.core.engine;

import java.util.ArrayList;
import java.util.List;
import java.util.Objects;
import java.util.concurrent.CompletableFuture;

/**
 * @author Jarvis
 * @date 2023/12/28 20:58
 */
public class NodeTask implements Runnable {

    private String name;
    private int time;

    private CompletableFuture<Void> future = CompletableFuture.runAsync(this);

    public CompletableFuture<Void> getFuture() {
        return future;
    }

    private List<NodeTask> parents = new ArrayList<>();
    private List<NodeTask> children = new ArrayList<>();

    public NodeTask(String name, int time) {
        this.name = name;
        this.time = time;
    }


    @Override
    public void run() {

        try {
            Thread.sleep(time * 1000);
        } catch (InterruptedException e) {
            e.printStackTrace();
        }
        System.out.println("执行任务：" + name + "，耗时：" + time + "秒");
    }

    @Override
    public String toString() {
        return name;
    }

    public String getName() {
        return name;
    }

    public List<NodeTask> getParents() {
        return parents;
    }

    public List<NodeTask> getChildren() {
        return children;
    }

    public boolean isLeaf() {
        return children.size() == 0;
    }

    @Override
    public boolean equals(Object o) {
        if (this == o) return true;
        if (o == null || getClass() != o.getClass()) return false;
        NodeTask nodeTask = (NodeTask) o;
        return Objects.equals(name, nodeTask.name);
    }

    @Override
    public int hashCode() {
        return Objects.hash(name);
    }
}
